package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.util.Set;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllCrawlers", query = "select myCrawler from Crawler myCrawler"),
		@NamedQuery(name = "findCrawlerByName", query = "select myCrawler from Crawler myCrawler where myCrawler.name = ?1"),
		@NamedQuery(name = "findCrawlerByNameContaining", query = "select myCrawler from Crawler myCrawler where myCrawler.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findCrawlerByUrl", query = "select myCrawler from Crawler myCrawler where myCrawler.url = ?1"),
		@NamedQuery(name = "findCrawlerByUrlContaining", query = "select myCrawler from Crawler myCrawler where myCrawler.url like concat('%',?1,'%')"),
		@NamedQuery(name = "findCrawlerBySnapshot", query = "select myCrawler from Crawler myCrawler where myCrawler.snapshot = ?1"),
		@NamedQuery(name = "findCrawlerBySnapshotContaining", query = "select myCrawler from Crawler myCrawler where myCrawler.snapshot like concat('%',?1,'%')"),
		@NamedQuery(name = "findCrawlerByDescription", query = "select myCrawler from Crawler myCrawler where myCrawler.description = ?1"),
		@NamedQuery(name = "findCrawlerByDescriptionContaining", query = "select myCrawler from Crawler myCrawler where myCrawler.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findCrawlerById", query = "select myCrawler from Crawler myCrawler where myCrawler.id = ?1"),
		@NamedQuery(name = "findCrawlerByPrimaryKey", query = "select myCrawler from Crawler myCrawler where myCrawler.id = ?1 "),
		@NamedQuery(name = "deleteCrawlersByIds", query = "delete Crawler where id in (?1)"),
		@NamedQuery(name = "findCrawlersByIds", query = "select myCrawler from Crawler myCrawler where myCrawler.id in (?1)"),
		@NamedQuery(name = "findCrawlerByPhyscialPrimaryKey", query = "select myCrawler from Crawler myCrawler where  myCrawler.name = ?1"),
		@NamedQuery(name = "countCrawlersByName", query = "select count(myCrawler)  from Crawler myCrawler where myCrawler.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "crawler")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Crawler")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Crawler implements Serializable, Comparable<Crawler> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "url")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String url;

	@Column(name = "snapshot")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String snapshot;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "crawler", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Crawlerpicture> crawlerpicturesForCrawler;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUrl() {
		return this.url;
	}

	public void setSnapshot(String snapshot) {
		this.snapshot = snapshot;
	}

	public String getSnapshot() {
		return this.snapshot;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setCrawlerpicturesForCrawler(Set<Crawlerpicture> crawlerpicturesForCrawler) {
		this.crawlerpicturesForCrawler = crawlerpicturesForCrawler;
	}

	public Set<Crawlerpicture> getCrawlerpicturesForCrawler() {
		if (crawlerpicturesForCrawler == null) {
			crawlerpicturesForCrawler = new java.util.LinkedHashSet<Crawlerpicture>();
		}
		return crawlerpicturesForCrawler;
	}

	public void copy(Crawler that) {

		setId(that.getId());

		setName(that.getName());
		setUrl(that.getUrl());
		setSnapshot(that.getSnapshot());
		setDescription(that.getDescription());

		setCrawlerpicturesForCrawler(new java.util.LinkedHashSet<Crawlerpicture>(
				that.getCrawlerpicturesForCrawler()));

	}

	public Crawler() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("url=[").append(url).append("] ");
		buffer.append("snapshot=[").append(snapshot).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Crawler))
			return false;
		Crawler comparedCrawler = (Crawler) obj;
		if (!CheckPropertyEqual(this.name, comparedCrawler.name)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Crawler o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
